home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 28
/
Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso
/
Aminet
/
game
/
board
/
Crafty-15.19.lha
/
crafty-15.19
/
src
/
iterate.c
< prev
next >
Wrap
C/C++ Source or Header
|
1998-09-13
|
17KB
|
415 lines
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "chess.h"
#include "data.h"
#include "epdglue.h"
/* last modified 07/08/98 */
/*
********************************************************************************
* *
* Iterate() is the routine used to drive the iterated search. it repeatedly *
* calls search, incrementing the search depth after each call, until either *
* time is exhausted or the maximum set search depth is reached. *
* *
********************************************************************************
*/
int Iterate(int wtm, int search_type, int root_list_done)
{
int *mvp;
int i, value=0, time_used;
int twtm, used_w, used_b;
int correct, correct_count, material=0, sorted, temp;
TREE *tree=local[0];
/*
----------------------------------------------------------
| |
| initialize. |
| |
| produce the root move list, which is ordered and kept |
| for the duration of this search (the order may change |
| as new best moves are backed up to the root of course.) |
| |
----------------------------------------------------------
*/
time_abort=0;
abort_search=0;
book_move=0;
program_start_time=ReadClock(time_type);
start_time=ReadClock(time_type);
cpu_time_used=0;
elapsed_start=ReadClock(elapsed);
PreEvaluate(tree,wtm);
tree->nodes_searched=0;
tree->fail_high=0;
tree->fail_high_first=0;
if (booking || !Book(tree,wtm,root_list_done)) do {
if (abort_search) break;
if (!root_list_done) RootMoveList(wtm);
if (EGTB_draw) EGTB_use=0;
else EGTB_use=EGTBlimit;
if (EGTBlimit && !EGTB_use)
Print(128,"Drawn at root, trying for swindle.\n");
correct_count=0;
burp=15*100;
transposition_id=(transposition_id+1)&7;
if (!transposition_id) transposition_id++;
program_start_time=ReadClock(time_type);
start_time=ReadClock(time_type);
cpu_percent=0;
elapsed_start=ReadClock(elapsed);
next_time_check=nodes_between_time_checks;
tree->evaluations=0;
#if !defined(FAST)
tree->transposition_hits=0;
tree->transposition_probes=0;
tree->pawn_probes=0;
tree->pawn_hits=0;
#endif
tree->tb_probes=0;
tree->tb_probes_successful=0;
tree->check_extensions_done=0;
tree->recapture_extensions_done=0;
tree->passed_pawn_extensions_done=0;
tree->one_reply_extensions_done=0;
root_wtm=wtm;
root_total_white_pieces=TotalWhitePieces;
root_total_white_pawns=TotalWhitePawns;
root_total_black_pieces=TotalBlackPieces;
root_total_black_pawns=TotalBlackPawns;
/*
----------------------------------------------------------
| |
| if there are no legal moves, it is either mate or draw |
| depending on whether the side to move is in check or |
| not (mate or stalemate.) |
| |
----------------------------------------------------------
*/
if (tree->last[0] == tree->last[1]) {
program_end_time=ReadClock(time_type);
tree->pv[0].path_length=0;
tree->pv[0].path_iteration_depth=10;
if (Check(wtm)) {
root_value=-(MATE-1);
last_search_value=-(MATE-1);
}
else {
root_value=DrawScore(crafty_is_white);
last_search_value=DrawScore(crafty_is_white);
}
Print(6," depth time score variation\n");
Print(6," (no moves)\n");
tree->nodes_searched=1;
return(root_value);
}
/*
----------------------------------------------------------
| |
| now set the search time and iteratively call Search() |
| to analyze the position deeper and deeper. note that |
| Search() is called with an alpha/beta window roughly |
| 1/3 of a pawn on either side of the score last |
| returned by search. also, after the first root move |
| is searched, this window is collapsed to n and n+1 |
| (where n is the value for the first root move.) often |
| a better move is found, which causes search to return |
| <beta> as the score. we then relax beta depending on |
| its value: if beta = alpha+1, set beta to alpha+1/3 |
| of a pawn; if beta = alpha+1/3 pawn, then set beta to |
| + infinity. |
| |
----------------------------------------------------------
*/
TimeSet(search_type);
iteration_depth=1;
if (last_pv.path_iteration_depth > 1)
iteration_depth=last_pv.path_iteration_depth+1;
Print(6," depth time score variation (%d)\n",
iteration_depth);
time_abort=0;
abort_search=0;
program_start_time=ReadClock(time_type);
start_time=ReadClock(time_type);
elapsed_start=ReadClock(elapsed);
/*
----------------------------------------------------------
| |
| now install the learned position information in the |
| hash table. |
| |
----------------------------------------------------------
*/
LearnPositionLoad(tree);
/*
----------------------------------------------------------
| |
| set the initial search bounds based on the last search |
| or default values. |
| |
----------------------------------------------------------
*/
tree->pv[0]=last_pv;
if (iteration_depth > 1) {
root_alpha=last_value-40;
root_value=last_value-40;
root_beta=last_value+40;
}
else {
root_alpha=-MATE-1;
root_value=-MATE-1;
root_beta=MATE+1;
}
for (i=0;i<256;i++) tree->root_nodes[i]=0;
/*
----------------------------------------------------------
| |
| if we are using multiple threads, and they have not |
| been started yet, then start them now as the search |
| is ready to begin. |
| |
----------------------------------------------------------
*/
#if defined(SMP)
if (max_threads>smp_idle+1) {
pthread_t pt;
int proc;
if (!EGTB_use || !(TotalPieces<=5 && EGTBScore(tree, 1, wtm, &i))) {
for (proc=smp_threads+1;proc<max_threads;proc++) {
Print(128,"starting thread %d\n",proc);
thread[proc]=0;
tfork(pt,ThreadInit,(void *) proc);
smp_threads++;
}
}
}
#endif
for (;iteration_depth<=60;iteration_depth++) {
/*
----------------------------------------------------------
| |
| now install the old PV into the hash table so that |
| these moves will be followed first. |
| |
----------------------------------------------------------
*/
twtm=wtm;
for (i=1;i<=(int) tree->pv[0].path_length;i++) {
tree->pv[i]=tree->pv[i-1];
if (!LegalMove(tree,i,twtm,tree->pv[i].path[i])) {
Print(4095,"ERROR, not installing bogus move at ply=%d\n",i);
Print(4095,"not installing from=%d to=%d piece=%d\n",
From(tree->pv[i].path[i]), To(tree->pv[i].path[i]),
Piece(tree->pv[i].path[i]));
break;
}
StorePV(tree, i, twtm);
MakeMove(tree, i,tree->pv[0].path[i],twtm);
twtm=ChangeSide(twtm);